/*
 * Copyright (C) 2011-2018 ShenZhen iBOXSaaS Information Technology Co.,Ltd.
 * 
 * All right reserved.
 * 
 * This software is the confidential and proprietary information of iBOXSaaS Company of China.
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the contract agreement you entered into with iBOXSaaS inc.
 * 
 */

package com.iboxpay.open.auth.center.handler;

import javax.servlet.http.HttpServletRequest;

import org.iboxpay.open.common.constants.LogEnums;
import org.iboxpay.open.common.constants.ResultEnums;
import org.iboxpay.open.common.exception.BusinessException;
import org.iboxpay.open.common.exception.ParamValidateException;
import org.iboxpay.open.common.utils.AccessLogUtils;
import org.iboxpay.open.common.vo.BaseResponseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iboxpay.common.json.MapperUtils;

/**
 * 
 * GlobalExceptionHandler.java
 * 
 * @author xuxiao
 * @date 2018/06/20
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger interfaceLog = LoggerFactory.getLogger(LogEnums.interfaceLog.getCode());
    private static final Logger accessLog = LoggerFactory.getLogger(LogEnums.accessLog.getCode());

    @ExceptionHandler(value = ParamValidateException.class)
    @ResponseBody
    public BaseResponseVo allExceptionHandler(HttpServletRequest request, ParamValidateException e) {
        BaseResponseVo baseResponse = new BaseResponseVo();
        baseResponse.setResultCode(e.getResultEnums().getCode());
        baseResponse.setErrorDesc(e.getResultEnums().getDesc());
        interfaceLog.error("[{}] [HTTP] [response] - Exception:", request.getRequestURI(), e);
        interfaceLog.info("[{}] [HTTP] [response] - {}", request.getRequestURI(), MapperUtils.toJson(baseResponse));
        accessLog.info(MapperUtils.toJson(AccessLogUtils.getAccessMap(Long.parseLong(MDC.get(LogEnums.startTime.getCode())),
                System.currentTimeMillis(), MDC.get(LogEnums.serviceId.getCode()),
                MDC.get(LogEnums.transactionId.getCode()), LogEnums.openAuth.getCode(), baseResponse, request)));
        return baseResponse;

    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public BaseResponseVo allExceptionHandler(HttpServletRequest request, BusinessException e) {
        BaseResponseVo baseResponse = new BaseResponseVo();
        baseResponse.setResultCode(e.getResultEnums().getCode());
        baseResponse.setErrorDesc(e.getResultEnums().getDesc());
        interfaceLog.error("[{}] [HTTP] [response] - Exception:", request.getRequestURI(), e);
        interfaceLog.info("[{}] [HTTP] [response] - {}", request.getRequestURI(), MapperUtils.toJson(baseResponse));
        accessLog.info(MapperUtils.toJson(AccessLogUtils.getAccessMap(Long.parseLong(MDC.get(LogEnums.startTime.getCode())),
            System.currentTimeMillis(), MDC.get(LogEnums.serviceId.getCode()),
            MDC.get(LogEnums.transactionId.getCode()), LogEnums.openAuth.getCode(), baseResponse, request)));
        return baseResponse;

    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponseVo allExceptionHandler(HttpServletRequest request, Exception e) {
        BaseResponseVo baseResponse = new BaseResponseVo();
        baseResponse.setResultCode(ResultEnums.authCenterError.getCode());
        baseResponse.setErrorDesc(ResultEnums.authCenterError.getDesc());
        interfaceLog.error("[{}] [HTTP] [response] - Exception:", request.getRequestURI(), e);
        interfaceLog.info("[{}] [HTTP] [response] - {}", request.getRequestURI(), MapperUtils.toJson(baseResponse));
        accessLog.info(MapperUtils.toJson(AccessLogUtils.getAccessMap(Long.parseLong(MDC.get(LogEnums.startTime.getCode())),
            System.currentTimeMillis(), MDC.get(LogEnums.serviceId.getCode()),
            MDC.get(LogEnums.transactionId.getCode()), LogEnums.openAuth.getCode(), baseResponse, request)));
        return baseResponse;
    }

    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public BaseResponseVo allThrowableHandler(HttpServletRequest request, Exception e) {
        BaseResponseVo baseResponse = new BaseResponseVo();
        baseResponse.setResultCode(ResultEnums.authCenterError.getCode());
        baseResponse.setErrorDesc(ResultEnums.authCenterError.getDesc());
        interfaceLog.error("[{}] [HTTP] [response] - Exception:", request.getRequestURI(), e);
        interfaceLog.info("[{}] [HTTP] [response] - {}", request.getRequestURI(), MapperUtils.toJson(baseResponse));
        accessLog.info(MapperUtils.toJson(AccessLogUtils.getAccessMap(Long.parseLong(MDC.get(LogEnums.startTime.getCode())),
            System.currentTimeMillis(), MDC.get(LogEnums.serviceId.getCode()),
            MDC.get(LogEnums.transactionId.getCode()), LogEnums.openAuth.getCode(), baseResponse, request)));
        return baseResponse;
    }

}
